package net.qiqbframework.modelling.command.voucher;


import lombok.Setter;
import net.qiqbframework.modelling.command.BizIdentifierVoucherMember;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 加载凭证可以有多个业务ID加载
 */
public class LoadVoucher {

    private final List<BizIdentifierVoucherMember> bizIdentifierMembers = new ArrayList<>();
    /**
     * 版本号
     */
    @Setter
    private Long version;


    public LoadVoucher(Long version) {

        this.version = version;
    }

    public void addBizIdentifierMember(BizIdentifierVoucherMember bizIdentifierMember) {
        if (!this.bizIdentifierMembers.contains(bizIdentifierMember)) {
            this.bizIdentifierMembers.add(bizIdentifierMember);
        }

    }

    /**
     * 获取主要的业务ID加载信息，用于加载聚合对象
     *
     * @return
     */
    public Optional<BizIdentifierVoucherMember> getPrimaryBizIdentifierMember() {
        return this.bizIdentifierMembers.stream().filter(bm -> !bm.isEmpty()).findFirst();
    }

    /**
     * 获取候选businessId
     *
     * @return
     */
    public List<BizIdentifierVoucherMember> listSubBizIdentifierMembers() {
        final Optional<BizIdentifierVoucherMember> primaryBizIdentifierMember = getPrimaryBizIdentifierMember();
        // 过滤
        return primaryBizIdentifierMember.map(bizIdentifierMember -> this.bizIdentifierMembers.stream().filter(bm -> !bm.isEmpty())
                .filter(bm -> bizIdentifierMember != bm).toList()).orElseGet(List::of);
    }

    public Long getVersion() {
        // 加载版本号
        return version;
    }

}
